//
//  UICollectionView+CJEnableMove.m
//  TSListDemo
//
//  Created by ciyouzen on 2020/3/26.
//  Copyright © 2020 dvlproad. All rights reserved.
//

#import "UICollectionView+CJEnableMove.h"
#import <objc/runtime.h>

#import "UIView+CJLongPressHandle.h"
#import "UICollectionView+CJLongPressMove.h"

@interface UICollectionView () {
    
}
@property (nonatomic, assign) BOOL cjContainShakeGR;                                        /**< 是否包含抖动手势 */

@end

@implementation UICollectionView (CJEnableMove)

#pragma mark - runtime Data
// cjContainShakeGR：是否包含抖动手势
- (BOOL)cjContainShakeGR {
    return [objc_getAssociatedObject(self, @selector(cjContainShakeGR)) boolValue];
}

- (void)setCjContainShakeGR:(BOOL)cjContainShakeGR {
    objc_setAssociatedObject(self, @selector(cjContainShakeGR), @(cjContainShakeGR), OBJC_ASSOCIATION_ASSIGN);
}


#pragma mark - Event
/*
*  让集合视图本身就可以通过长按来进行拖动(而不用有其他如编辑的按钮来启动拖动)，并且可选择长按后(即拖动前)是否要有抖动效果
*
*  @param containShakeEffect        长按后(即拖动前)是否要有抖动效果
    (如果要有抖动效果除了这里containShakeEffect参数传YES外，cellForItemAtIndexPath中还必须
    if (collectionView.cjShakeType == CJShakeTypeMoving) {
        [cell cjShakeKeeping];
 *  @param checkSectionMoveEnableBlock   设置允许哪些section中的哪些item进行拖动操作，未设置则默认都允许
 *  @param checkCellMoveEnableBlock      检查是否允许跨secton操作，未设置则默认允许
 *  @param cellMoveCompleteBlock         移动结束的回调
    })
*/
- (void)cj_enableMoveByLongPressWithShakeEffect:(BOOL)containShakeEffect
                    checkSectionMoveEnableBlock:(BOOL (^ __nullable)(NSIndexPath *indexPath))checkSectionMoveEnableBlock
                       checkCellMoveEnableBlock:(BOOL (^ __nullable)(NSInteger fromSection, NSInteger toSection))checkCellMoveEnableBlock
                          cellMoveCompleteBlock:(void(^ __nullable)(NSIndexPath *sourceIndexPath, NSIndexPath *destinationIndexPath))cellMoveCompleteBlock
{
    self.cjContainShakeGR = containShakeEffect;
    
    [self __addGestureRecognizerWithContainShakeGR:containShakeEffect];
    
    [self cj_setupCheckSectionMoveEnableBlock:checkSectionMoveEnableBlock
                     checkCellMoveEnableBlock:checkCellMoveEnableBlock
                        cellMoveCompleteBlock:^(NSIndexPath * _Nonnull sourceIndexPath, NSIndexPath * _Nonnull destinationIndexPath) {
        if (self.cjContainShakeGR) {
            self.cjShakeType = CJShakeTypeEnd;
            [self reloadData];
            
            if ([self.gestureRecognizers containsObject:self.cjLongPressHandleGR] == NO) {
                [self addGestureRecognizer:self.cjLongPressHandleGR];
                if ([self.gestureRecognizers containsObject:self.cjMoveGestureRecognizer] == YES) {
                    [self removeGestureRecognizer:self.cjMoveGestureRecognizer];
                }
            }
        }
        
        
        !cellMoveCompleteBlock ?: cellMoveCompleteBlock(sourceIndexPath, destinationIndexPath);
    }];
}

/// 添加Move拖动手势（可选择是否包含抖动手势）
- (void)__addGestureRecognizerWithContainShakeGR:(BOOL)containShakeGR {
    if (containShakeGR == NO) {
        [self __addMoveGestureRecognizer];
    } else {
        // 添加长按重绘reload使其cell抖动的手势
        [self cj_addLongPressGRWithBeganHandle:^{
            self.cjShakeType = CJShakeTypeMoving;
            [self reloadData];
            
            //添加其他手势后，即使没remove本手势，本手势也会被盖住，即本手势的endHandle也就无法执行到了
            [self __addMoveGestureRecognizer];
        } changeHandle:nil endHandle:^{
            //被其他手势盖住或者已remove，所以本手势的endHandle也就无法执行到了
        }];
    }
}



#pragma mark - Private Method
/// 添加长按拖动的手势
- (void)__addMoveGestureRecognizer {
    if (self.cjLongPressHandleGR) {
        [self removeGestureRecognizer:self.cjLongPressHandleGR];
    }
    
    NSTimeInterval minimumPressDuration = 1.0;
    if (self.cjShakeType == CJShakeTypeMoving) {
        minimumPressDuration = 0.1;
    }
    [self cj_addLongPressMoveGR:minimumPressDuration];
}


@end
